//
// Created by hsj on 2022/1/18.
//

#include <iostream>
#include <string>
#include "t_class_pointer.h"

namespace hsj_class_pointer{
    //测试指向对象的指针
    void tst_class_pointer(){
        using std::string;
        using std::cout;
        using std::endl;

        //堆缓存
        const int BUFF_SIZE = 512;
        char *buffer = new char[BUFF_SIZE];

        JustTesting *pjt1,*pjt2;
        pjt1 = new (buffer)JustTesting;
        pjt2 = new JustTesting("Heap1",20);

        cout<<" buffer addr: "<<(void*)buffer<<" heap addr:"<<pjt2<<endl;
        cout<<" content: "<<endl;
        cout<<pjt1<<" :"<<endl;
        pjt1->show();
        cout<<pjt2<<" :"<<endl;
        pjt2->show();

        JustTesting *pjt3,*pjt4;
        pjt3 = new (buffer + sizeof (JustTesting))JustTesting("bad idea.",6);
        pjt4 = new JustTesting("Heap2",10);
        cout<<" content v2: "<<endl;
        cout<<pjt3<<" :"<<endl;
        pjt3->show();
        cout<<pjt4<<" :"<<endl;
        pjt4->show();

        cout<<endl;
        delete pjt2;
        delete pjt4;

        //显示调用析构函数
        pjt1->~JustTesting();
        pjt3->~JustTesting();
        //然后释放缓存空间
        //just free buffer
        delete [] buffer;

    }
}